﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InternalManagementSystem.DAO.Models;

public partial class Admin_UserTypeList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["Permission"] != null)
            {
                Permission permission = Session["Permission"] as Permission;
                if (permission.IsUserTypeEdit)//违章
                {
                    Bind();
                }
            }
           
        }
    }

    void Bind()
    {
        AdminBLL bll = new AdminBLL();
        gvUserType.DataSource = bll.GetAllUserType();
        gvUserType.DataBind();
    }

    protected void gvUserType_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvUserType.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void gvUserType_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                int id = Convert.ToInt32(gvUserType.DataKeys[e.RowIndex].Value);
                AdminBLL bll = new AdminBLL(permission);
                bll.DeleteUserType(id);

                Response.Redirect("UserTypeList.aspx");
            }
        }
    }
    protected void gvUserType_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                try
                {
                    switch (e.CommandName)
                    {
                        case "Insert":
                            UserType d = new UserType();

                            d.Name = ((TextBox)gvUserType.FooterRow.FindControl("txtNewName")).Text;
                            d.GuardLine = Convert.ToInt32(((TextBox)gvUserType.FooterRow.FindControl("txtNewGuardLine")).Text);
                            AdminBLL bll = new AdminBLL(permission);
                            bll.AddUserType(d);
                            Response.Redirect("UserTypeList.aspx");
                            break;
                    }
                }
                catch (Exception ex)
                { }
            }
        }
       
    }
    protected void gvUserType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvUserType.EditIndex = -1;
        Bind();
    }
    protected void gvUserType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (Session["Permission"] != null)
        {
            Permission permission = Session["Permission"] as Permission;
            if (permission.IsUserTypeEdit)//违章
            {
                int id = Convert.ToInt32(gvUserType.DataKeys[e.RowIndex].Value);
                TextBox editName = gvUserType.Rows[e.RowIndex].FindControl("txtEditName") as TextBox;
                TextBox editGuardLine = gvUserType.Rows[e.RowIndex].FindControl("txtEditGuardLine") as TextBox;

                UserType bm = new UserType();
                bm.ID = id;
                bm.Name = editName.Text;
                bm.GuardLine = Convert.ToInt32(editGuardLine.Text.Trim());

                AdminBLL bll = new AdminBLL(permission);

                bool result = bll.UpdateUserType(bm);

                Response.Redirect("UserTypeList.aspx");
                
            }
        }
    }
}